Glasgow | Jan-26 | Elisabeth Matulian | Sprint 2 | Coursework#959
Open
Elisabeth-Matulian wants to merge 5 commits intoCodeYourFuture:mainfrom
Open
Glasgow | Jan-26 | Elisabeth Matulian | Sprint 2 | Coursework#959Elisabeth-Matulian wants to merge 5 commits intoCodeYourFuture:mainfrom
Elisabeth-Matulian wants to merge 5 commits intoCodeYourFuture:mainfrom
Conversation
cjyuan
reviewed
Feb 18, 2026
|
|
||
| // c) What is the return value of pad is called for the first time? | ||
| // =============> write your answer here | ||
| // =============> 00 |
Contributor
There was a problem hiding this comment.
To more precisely express a value is a string, we can enclose the value by a pair of double quotes. For example, "00".
|
|
||
| function formatAs12HourClock(time) { | ||
| const hours = Number(time.slice(0, 2)); | ||
| const mins = time.slice(3, 5); |
Contributor
There was a problem hiding this comment.
Could also use time.slice(-2) to extract two characters from the right (or last two characters).
| return `${hours - 12}:${mins} pm`; | ||
| } | ||
| else if (hours == 0) { | ||
| return `${hours + 12}:${mins} am`; |
Contributor
There was a problem hiding this comment.
0 +12 is 12. Why not just write 12 directly?
Comment on lines
8
to
+10
| if (hours > 12) { | ||
| return `${hours - 12}:00 pm`; | ||
| return `${hours - 12}:${mins} pm`; | ||
| } |
Contributor
There was a problem hiding this comment.
If "01:00" is converted to "01:00 am", it is probably reasonable for the caller to expect "13:00" to be converted to "01:00 pm".
When the returned values are not formatted consistently, it may result in unintended side-effect. For examples,
- When the strings are displayed, "01:00 pm" and "1:00 pm" would not align as nicely.
01:00 am
1:00 pm
12:00 am
01:00 pm
- When the formatted strings are compared in the program,
"1:00 pm" < "11:00 pm"and01:00 am" < "11:00 am"produce different results.
Consistency is important so the caller can be certain what to expect from a function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Self checklist
Changelist
Completed all exercises
Questions
No questions